/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：SysWebCfgContorller.java
 * History:
 *       May 13, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.contorller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tangkf.utils.bean.ReqHelper;
import com.xteam.ecsystem.entities.SysWebConfig;
import com.xteam.ecsystem.service.admin.SysWebCfgService;
import com.xteam.ecsystem.service.admin.SystemLogService;
import com.xteam.ecsystem.utils.ConstantLocation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 前端配置控制枢纽站
 * @author liuzhi
 *
 */
@Controller
public class SysWebCfgContorller {

	@Autowired
	private SysWebCfgService sysWebCfgService;
	
	@Autowired
	private SystemLogService logService;
	
	public void setToBeJsonType(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("application/json;charset=UTF-8");
	};
	
	/**
	 * 获得前端配置数据对象
	 * @author liuzhi 2013-12-18
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws IOException
	 */
	@RequestMapping("/getSysWebCfg.do")
	public void getGoodsTypeList(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		setToBeJsonType(request, response);
		
		SysWebConfig config = this.sysWebCfgService.getSysWebCfg();
		
		response.getWriter().write(JSONObject.fromObject(config).toString());
	}
	
	/**
	 * 保存前端配置信息 
	 * @author liuzhi 2013-12-19
	 * @param request
	 * @param response
	 * @param modelMap
	 * @throws IOException
	 */
	@RequestMapping("/saveSysWebCfg.do")
	public void saveSysWebCfg(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws IOException {
		String jsonStr = ReqHelper.getRequestString(request, "swcInfo");
		String lslUser = ReqHelper.getRequestString(request, "lslUser");
		String lslAddress = ReqHelper.getRequestString(request, "lslAddress");
		String lslType = ConstantLocation.LOG_ADD;
		
		JSONObject obj = JSONObject.fromObject(jsonStr);
		SysWebConfig webCfg = (SysWebConfig)JSONObject.toBean(obj, SysWebConfig.class);
		
		if(StringUtil.isNotNullOrEmpty(webCfg.getSwcId())){
			lslType = ConstantLocation.LOG_MODIFY;
		}
		
		this.logService.saveSystemLogs(lslUser, lslAddress, ConstantLocation.LOG_SYSWEBCFG, 
				"saveSysWebCfg", lslType, "SysWebConfig");
		
		this.sysWebCfgService.saveSysWebCfg(webCfg);
	}
	
}
